package ictbk.ttmk51.grapro.app.service;

import java.util.ArrayList;
import java.util.HashMap;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.AndroidHttpTransport;

import android.util.Log;
import ictbk.ttmk51.grapro.app.common.Constant;
import ictbk.ttmk51.grapro.app.safephonebook.BackupInfo;
import ictbk.ttmk51.grapro.app.safephonebook.Contact;
import ictbk.ttmk51.grapro.app.safesms.SMS;
import ictbk.ttmk51.grapro.app.utils.MyXStream;

public class GetWebservice implements Constant{
	private static String METHOD_NAME;
	
	public static String CheckLogin(String username, String password){
		METHOD_NAME = "Login";
		String Result = null;
		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
		request.addProperty("username", username);
		request.addProperty("password", password);
		
		
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		envelope.dotNet = true;
		envelope.setOutputSoapObject(request);
		
		AndroidHttpTransport aht = new AndroidHttpTransport(URL + METHOD_NAME);
		try{
			aht.call(NAMESPACE + METHOD_NAME, envelope);
			//aht.call(SOAP_ACTION, envelope);
			SoapPrimitive soapString = (SoapPrimitive) envelope.getResponse();
			Result = soapString.toString();
			Log.i("Kết quả trả về", Result.toString());
			
		} catch(Exception e){
			Log.i("Lỗi", e.getMessage());
			Log.w("Error", e.getMessage());
			Result = null;
		}
		return Result;
	}
	
	public static String CreateNewAccount(String username, String password, String email){
		METHOD_NAME = "CreateNewAccount";
		String Result = null;
		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
		request.addProperty("username", username);
		request.addProperty("password", password);
		request.addProperty("email", email);
		
		
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		envelope.dotNet = true;
		envelope.setOutputSoapObject(request);
		
		AndroidHttpTransport aht = new AndroidHttpTransport(URL + METHOD_NAME);
		try{
			aht.call(NAMESPACE + METHOD_NAME, envelope);
			//aht.call(SOAP_ACTION, envelope);
			SoapPrimitive soapString = (SoapPrimitive) envelope.getResponse();
			Result = soapString.toString();
			Log.i("Kết quả trả về", Result.toString());
			
		} catch(Exception e){
			Log.i("Lỗi", e.getMessage());
			Log.w("Error", e.getMessage());
			Result = "Error when attempting connect to server";
		}
		return Result;
	}
	
	public static String sendEmail(String to) throws Exception {
		String METHOD_NAME = "sendEmail";
		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

		request.addProperty("from", "tranhuuphuoc88@gmail.com");
		request.addProperty("to", to);

		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		envelope.dotNet = true;
		envelope.setOutputSoapObject(request);
		AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(URL + METHOD_NAME);

		androidHttpTransport.call(NAMESPACE + METHOD_NAME, envelope);
		SoapPrimitive result = (SoapPrimitive) envelope.getResponse();
		return result.toString();
	}

	/**
	 * Recover password from email
	 * @param username Get password and email from username
	 * @return HashMap<String, String> with key "email" to get value email, key "password" to get value password
	 * @throws Exception
	 */
	public static HashMap<String, String> getEmailPasswordFromUsername(String username)throws Exception{
		String METHOD_NAME = "GetEmailPasswordFromUsername";
		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

		request.addProperty("username", username);

		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		envelope.dotNet = true;
		envelope.setOutputSoapObject(request);
		AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(URL + METHOD_NAME);

		androidHttpTransport.call(NAMESPACE + METHOD_NAME, envelope);
		SoapPrimitive result = (SoapPrimitive) envelope.getResponse();
		HashMap<String, String> email_password = new HashMap<String, String>();
		String[] temp = result.toString().split("/");
		if(temp==null||temp.length!=2){
			return null;
		}
		email_password.put("email", temp[0]);
		email_password.put("password", temp[1]);
		return email_password;
	}
	
	public static String getAccountConnectedToEmail(String email) throws Exception {
		String METHOD_NAME = "GetAccountFromEmail";
		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

		request.addProperty("email", email);

		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		envelope.dotNet = true;
		envelope.setOutputSoapObject(request);
		AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(URL + METHOD_NAME);

		androidHttpTransport.call(NAMESPACE + METHOD_NAME, envelope);
		SoapPrimitive result = (SoapPrimitive) envelope.getResponse();
		return result.toString().trim();
	}
	
	public static String RevertPassword(String email){
		METHOD_NAME = "RevertPassword";
		String Result = null;
		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
		request.addProperty("email", email);
		
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		envelope.dotNet = true;
		envelope.setOutputSoapObject(request);
		
		AndroidHttpTransport aht = new AndroidHttpTransport(URL + METHOD_NAME);
		try{
			aht.call(NAMESPACE + METHOD_NAME, envelope);
			//aht.call(SOAP_ACTION, envelope);
			SoapPrimitive soapString = (SoapPrimitive) envelope.getResponse();
			Result = soapString.toString();
			Log.i("Kết quả trả về", Result.toString());
			
		} catch(Exception e){
			Log.i("Lỗi", e.getMessage());
			Log.w("Error", e.getMessage());
			Result = "Error when attempting connect to server";
		}
		return Result;
	}
	
	public static boolean changePassword(String usernameCurrent,String newPassword) throws Exception {
		String METHOD_NAME = "ChangePassword";
		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

		request.addProperty("usernameCurrent", usernameCurrent);
		request.addProperty("newPassword", newPassword);

		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		envelope.dotNet = true;
		envelope.setOutputSoapObject(request);
		AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(URL + METHOD_NAME);

		androidHttpTransport.call(NAMESPACE + METHOD_NAME, envelope);
		SoapPrimitive result = (SoapPrimitive) envelope.getResponse();
		String myResult = result.toString();
		return myResult.equalsIgnoreCase("true");
	}
	
	public static boolean backupPhoneBook(ArrayList<Contact> list, int user_id, String date, String time, String note)throws Exception{
		METHOD_NAME = "BackupContact";
		String Result =null;
		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
		
		String content = MyXStream.ContactListtoXML(list);
		request.addProperty("xml", content);
		request.addProperty("user_id", user_id);
		request.addProperty("date", date);
		request.addProperty("time", time);
		request.addProperty("note", note);
		
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		envelope.dotNet = true;
		envelope.setOutputSoapObject(request);
		
		AndroidHttpTransport aht = new AndroidHttpTransport(URL + METHOD_NAME);
		aht.call(NAMESPACE + METHOD_NAME, envelope);
		SoapPrimitive soapString = (SoapPrimitive)envelope.getResponse();
		Result = soapString.toString();
		return Result.equalsIgnoreCase("true");
	}
	
	public static ArrayList<BackupInfo> getAllBackupInfo(int user_id)throws Exception{
		METHOD_NAME = "GetAllBackupInfo";
		
		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
		
		request.addProperty("user_id", user_id);
		SoapSerializationEnvelope envelope =  new SoapSerializationEnvelope(SoapEnvelope.VER11);
		envelope.dotNet = true;
		envelope.setOutputSoapObject(request);
		
		AndroidHttpTransport aht = new AndroidHttpTransport(URL + METHOD_NAME);
		aht.call(NAMESPACE + METHOD_NAME, envelope);
		SoapObject resultRequestSOAP = (SoapObject) envelope.bodyIn;
		SoapObject a = (SoapObject) resultRequestSOAP.getProperty(0);
		int count = a.getPropertyCount();
		Log.i("Get backup Info", "Size: "+count);
		ArrayList<BackupInfo> bki = new ArrayList<BackupInfo>();
		for(int i = 0; i < count; i++){
			SoapObject so = (SoapObject) a.getProperty(i);
			String date = so.getProperty("date").toString();
			String time = so.getProperty("time").toString();
			String notes = so.getProperty("notes").toString().trim();
			String id = so.getProperty("id").toString();
			bki.add(new BackupInfo(date, time, notes, id));
		}
		return bki;
	}
	
	public static boolean deleteBackupVersion(String id) throws Exception{
		METHOD_NAME = "DeleteBackupVersion";
		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
		request.addProperty("id", id);
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		envelope.dotNet = true;
		envelope.setOutputSoapObject(request);
		
		AndroidHttpTransport aht = new AndroidHttpTransport(URL + METHOD_NAME);
		aht.call(NAMESPACE + METHOD_NAME, envelope);
		SoapPrimitive soapString= (SoapPrimitive)envelope.getResponse();
		String Result = soapString.toString();
		
		return Boolean.parseBoolean(Result);
	}
	public static ArrayList<Contact> restoreContactList(String version_id)throws Exception {
		METHOD_NAME = "RestoreContact";
		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
		request.addProperty("version_id", version_id);
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		envelope.dotNet = true;
		envelope.setOutputSoapObject(request);
		
		AndroidHttpTransport aht = new AndroidHttpTransport(URL + METHOD_NAME);
		aht.call(NAMESPACE+ METHOD_NAME, envelope);
		SoapPrimitive soapString = (SoapPrimitive) envelope.getResponse();
		String Result = soapString.toString();
		Log.i("Reverting...", Result);
		if(Result.trim().equals("")){
			return null;
		}
		return MyXStream.getContactListfromXML(Result);
	}
	
	public static boolean backupSMS(ArrayList<SMS> SMSList, int user_id, String sms_date, String sms_time, String note)throws Exception{
		METHOD_NAME = "BackupSMS";
		String Result = null;
		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
		
		String content = MyXStream.SMSListtoXML(SMSList);
		request.addProperty("xml", content);
		request.addProperty("user_id", user_id);
		request.addProperty("sms_date", sms_date);
		request.addProperty("sms_time", sms_time);
		request.addProperty("sms_note", note);
		
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		envelope.dotNet = true;
		envelope.setOutputSoapObject(request);
		
		AndroidHttpTransport aht = new AndroidHttpTransport(URL + METHOD_NAME);
		aht.call(NAMESPACE + METHOD_NAME, envelope);
		SoapPrimitive soapString = (SoapPrimitive)envelope.getResponse();
		Result = soapString.toString();
		return Result.equalsIgnoreCase("true");
	}
	
	public static ArrayList<BackupInfo> getAllSMSBackupInfo(int user_id)throws Exception{
		METHOD_NAME = "GetAllSMSBackupInfo";
		
		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
		
		request.addProperty("user_id", user_id);
		SoapSerializationEnvelope envelope =  new SoapSerializationEnvelope(SoapEnvelope.VER11);
		envelope.dotNet = true;
		envelope.setOutputSoapObject(request);
		
		AndroidHttpTransport aht = new AndroidHttpTransport(URL + METHOD_NAME);
		aht.call(NAMESPACE + METHOD_NAME, envelope);
		SoapObject resultRequestSOAP = (SoapObject) envelope.bodyIn;
		SoapObject a = (SoapObject) resultRequestSOAP.getProperty(0);
		int count = a.getPropertyCount();
		Log.i("Get sms backup Info", "Size: "+count);
		ArrayList<BackupInfo> bki = new ArrayList<BackupInfo>();
		for(int i = 0; i < count; i++){
			SoapObject so = (SoapObject) a.getProperty(i);
			String date = so.getProperty("date").toString();
			String time = so.getProperty("time").toString();
			String notes = so.getProperty("notes").toString().trim();
			String id = so.getProperty("id").toString();
			bki.add(new BackupInfo(date, time, notes, id));
		}
		return bki;
	}
	
	public static boolean deleteSMSBackupVersion(String id) throws Exception{
		METHOD_NAME = "DeleteSMSBackupVersion";
		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
		request.addProperty("sms_backup_id", id);
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		envelope.dotNet = true;
		envelope.setOutputSoapObject(request);
		
		AndroidHttpTransport aht = new AndroidHttpTransport(URL + METHOD_NAME);
		aht.call(NAMESPACE + METHOD_NAME, envelope);
		SoapPrimitive soapString= (SoapPrimitive)envelope.getResponse();
		String Result = soapString.toString();
		
		return Boolean.parseBoolean(Result);
	}
	
	public static ArrayList<SMS> restoreSMSList(String sms_backup_id)throws Exception {
		METHOD_NAME = "RestoreSMS";
		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
		request.addProperty("sms_backup_id", sms_backup_id);
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		envelope.dotNet = true;
		envelope.setOutputSoapObject(request);
		
		AndroidHttpTransport aht = new AndroidHttpTransport(URL + METHOD_NAME);
		aht.call(NAMESPACE+ METHOD_NAME, envelope);
		SoapPrimitive soapString = (SoapPrimitive) envelope.getResponse();
		String Result = soapString.toString();
		Log.i("Reverting...", Result);
		if(Result.trim().equals("")){
			return null;
		}
		return MyXStream.getSMSListfromXML(Result);
	}
	
	public static boolean BackupFile(String stringBase64, String filename, int user_id, String file_date, String file_time, String file_notes)throws Exception{
		METHOD_NAME = "BackupFile";
		String Result = null;
		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
		
		request.addProperty("data", stringBase64);
		request.addProperty("filename", filename);
		request.addProperty("user_id", user_id);
		request.addProperty("date", file_date);
		request.addProperty("time", file_time);
		request.addProperty("notes", file_notes);
		
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		envelope.dotNet = true;
		envelope.setOutputSoapObject(request);
		
		AndroidHttpTransport aht = new AndroidHttpTransport(URL + METHOD_NAME);
		aht.call(NAMESPACE + METHOD_NAME, envelope);
		SoapPrimitive soapString = (SoapPrimitive)envelope.getResponse();
		Result = soapString.toString();
		return Result.equalsIgnoreCase("true");
	}
}
